package org.ezplatform.office.meeting.common.util;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.collections4.MapUtils;
import org.ezplatform.cloud.common.service.ConfigService;
import org.ezplatform.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

@Service
public class MeetingUtils {
	private final static Logger LOGGER = LoggerFactory.getLogger(MeetingUtils.class);
	@Autowired
    private ConfigService configService;
	
	private static ThreadPoolTaskExecutor executor;
	
	private static final Object executorMonitor = new Object();

	/**
     * 根据参数名获取系统设置值
     * @param corpId
     * @param token
     * @return
     */
//	public String getSysParamValueByMark(String corpId, String token, String mark) {
//		
//		RestClient restClient = new RestClient(true);
//		Map<String, String> paramMap = new HashMap<String, String>();
//		paramMap.put("corpId", corpId);
//		paramMap.put("mark", mark);
//		String jsonStr = restClient.get("/system/config/getConfigValue", token, paramMap);
//		Map<String, Object> result = JsonUtils.readValue(jsonStr);
//		if (MapUtils.isNotEmpty(result)) {
//			if ("success".equals(result.get("type"))) {
//				return String.valueOf(result.get("data"));
//			}
//		}
//		return "";
//	}
	
	/**
	 * 获取应用中的消息设置
	 * @param corpId
	 * @param token
	 * @param mark
	 * @return
	 */
	public boolean getAppInfoMessageSet(String corpId, String token, String mark) {
		String reminderAction = "";
    	Map<String, Object> appInfo = configService.getAppInfoByAppCode("meeting", corpId, token);
    	if(MapUtils.isNotEmpty(appInfo)) 
    		reminderAction = StringUtils.null2String(appInfo.get("reminderAction"));
    	if(reminderAction.contains(mark)) return true;
    	return false;
	}
    
	/**
	 * 获取消息推送的方式
	 * @param corpId
	 * @param token
	 * @return
	 */
//    public String getMessagePush(String corpId, String token) {
//    	LOGGER.debug("-----getMessagePush----");
//    	String result = "";
//    	String reminderAction = "";
//    	//获取应用中心中的设置
//    	Map<String, Object> appInfo = configService.getAppInfoByAppCode("meeting", corpId, token);
//    	if(MapUtils.isNotEmpty(appInfo)) reminderAction = StringUtils.null2String(appInfo.get("reminderAction"));
//    	LOGGER.debug("--reminderAction:"+reminderAction);
//    	//获取系统设置中的设置
//    	String evoRemind = getSysParamValueByMark(corpId, token, "evoRemind");
//    	String mobile = getSysParamValueByMark(corpId, token, "sysImType");
//    	String email = getSysParamValueByMark(corpId, token, "sysMailSetting");
//    	String sms = getSysParamValueByMark(corpId, token, "sysSMSsetting");
//    	LOGGER.debug("--evoRemind:"+evoRemind+"--mobile:"+mobile+"--email:"+email+"--sms:"+sms);
// 
//    	if(reminderAction.contains("mobile") && StringUtils.isNotBlank(evoRemind) && !"0".equals(evoRemind)) {
//    		result += "mobile|"+evoRemind+"|,";
//    	}
//    	if(reminderAction.contains("email") && StringUtils.isNotBlank(email) && !"0".equals(email)) {
//    		result += "email,";
//    	}
//    	if(reminderAction.contains("sms") && StringUtils.isNotBlank(sms) && !"0".equals(sms)) {
//    		result += "sms|"+sms+"|,";
//    	}
//		return result;
//    }
    
    /**
     * 
     * @param column 列字段 eg: org
     * @param values 列表 eg: [1,2,3,4,5,6]
     * @param num 每批次数量 eg: 3
     * @return where in 子句  eg: (org in( 1,2,3) or org in(4,5,6))
     */
    public static String getWhereInValuesSql(String column, List<String> values,int num) {
    	if(values==null||values.isEmpty()) {
    		return "(1=1)";
    	}
        String sql = "(";
        int valueSize = values.size();
        int batchSize = valueSize / num + (valueSize % num == 0 ? 0 : 1);
        for (int i = 0; i < batchSize; i++) {
            if (i > 0) {
                sql += ") or ";
            }
            sql += column+" in (";
            for (int j = i * num; ( j < (i + 1) * num) && j < valueSize; j++) {
                if (j > i * num) {
                    sql += ",";
                }
                sql += "'" + values.get(j) + "'";
            }
        }
        sql += "))";
        return sql;
    }
    
    /**
     * 执行线程
     * @param runnable
     */
    public static void execute(Runnable runnable) {
    	if(executor == null) {
    		synchronized (executorMonitor) {
    			executor = new ThreadPoolTaskExecutor();
        		executor.setCorePoolSize(5);
        		executor.setMaxPoolSize(200);
        		executor.setQueueCapacity(10);
        		executor.setThreadNamePrefix("meetingAsyncTaskExecutor-");
        		executor.initialize();
			}
    	}
    	executor.execute(runnable);
    }
    
}
